<%--
  Created by IntelliJ IDEA.
  User: WangWeiwei
  Date: 16-10-22
  Time: 下午3:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path;
    request.setAttribute("basePath", basePath);
%>
<div class="top-grids">
    <div style="margin: 50px;"></div>
    <h3>${web['title']}
        <button id="pushNewGoods" class="btn btn-success">发布商品</button>
    </h3>
    <div>
        <div class="panel panel-default" id="conditions-for-screening-panel0">
            <div class="panel-heading">展示图片</div>

            <div class="panel-body">
                <button id="btn-upload-picture" class="btn btn-primary">上传图片</button>
                <div type="text/plain" id="upload-picture" class="span12" style="height: 200px;">
                </div>
                <script>
                    var $uploadPicture;
                    $(function () {
                        var uploadPicture = UM.getEditor("upload-picture",{toolbar:['image']});
                        uploadPicture.setHide();
                        $uploadPicture = uploadPicture;
                        $("#btn-upload-picture").click(function () {
                            uploadPicture.show();
                            $("#conditions-for-screening-panel0").find("div[data-original-title='图片']").click();

                        });
                    })
                </script>
            </div>
        </div>
        <div class="panel panel-default" id="conditions-for-screening-panel1">
            <div class="panel-heading">基本属性</div>
            <div class="panel-body">
                <%@include file="add_goods_select.jsp"%>
            </div>
        </div>
        <div class="panel panel-default" id="conditions-for-screening-panel2">
            <div class="panel-heading">更多信息</div>
            <div class="panel-body">

                <div>
                    <%--ue编辑器--%>
                        <%--编辑器--%>
                    <script>
                        //1.设置编辑器配置文件的根路径
                        window.UMEDITOR_HOME_URL = '${basePath}/resources/umeditor/';
                    </script>
                    <%@include file="../../utils/umeditor.jsp"%>
                    <script>

                        $(function () {
                            //2.初始化编辑器的内容
                            um${editorId}.setContent('在这里编辑商品的详情信息');
                        })
                    </script>

                    <script id="my"></script>
                </div>
            </div>
        </div>

    </div>
</div>
<script>
    /**计算商品类型id*/
    var getGoodsTypeId = function (goods_type1,goods_type2) {
        if (goods_type2 == -1){
            return goods_type1;
        }else {
            return  goods_type2;
        }
    };
    /**计算商品适用作物或适用症状*/
    var getBehhoof = function (goods_crops) {
        var jsonStr = "[";
                for (var i in goods_crops) {
                    jsonStr += "{\"id\":" + goods_crops[i] + "}";
                    if (i < goods_crops.length - 1) {
                        jsonStr += ","
                    }
                }
            jsonStr += "]";
        return JSON.parse(jsonStr);
    };
    $(function(){
        function jsonStringToObj(goods_pictures, goods_name,
                                 goods_type1, goods_type2,
                                 goods_crops, goods_symptons,
                                 goods_standards, goods_code,
                                 goods_productor, goods_describe) {
            var obj = {
                pictureHtml:"",
                goods:{
                    goodsName:  goods_name ,
                    type:{id:getGoodsTypeId(goods_type1, goods_type2)},
                    standard : goods_standards ,
                    code: goods_code ,
                    productor:{name: goods_productor} ,
                    describe :""
                },
                crops: [],
                symptons:[]
            };
            obj.crops = getBehhoof(goods_crops);
            obj.symptons = getBehhoof(goods_symptons);
            obj.pictureHtml = goods_pictures;
            obj.goods.describe = goods_describe;
            return obj;
        }
        function getGoodsMessage() {
            //商品图片
            var goods_pictures = $uploadPicture.getContent();
            //商品名
            var goods_name = $("#add-goods-goods-name").val();
            //商品类型1
            var goods_type1 = $("#add-goods-select-goods-type1").val();
            //商品类型2
            var goods_type2 = $("#add-goods-select-goods-type2").val();
            //作用作物 其值的实例 ["-1", "5", "4"]
            var goods_crops = $("#add-goods-crops").val();
            //适用症状
            var goods_symptons = $("#add-goods-sympton").val();
            //商品规格
            var goods_standards = $("input[placeholder='规格']").val();
            //商品条码
            var goods_code = $("input[placeholder='条码']").val();
            //生产厂商
            var goods_productor = $("#add-goods-productor").val();
            //商品详情信息
            var goods_describe = um.getContent();
            //发布商品参数
            param = jsonStringToObj(goods_pictures, goods_name, goods_type1, goods_type2, goods_crops, goods_symptons, goods_standards, goods_code, goods_productor, goods_describe)
        }
        var param;
        $("#pushNewGoods").click(function () {
            getGoodsMessage();
            console.log(param);
            $.ajax(
                    {
                        url: "${basePath}/manager/goods/push_new_goods",
                        contentType : "application/json",
                        data : JSON.stringify(param),
                        type : "POST",
                        success: function (data) {
                            if (data == 1) {
                            }
                            else {
                                //商品发布失败,给用户提示
                            }
                        }
                    })
        })
    })
</script>